<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/11/3
 * Time: 22:00
 */
namespace app\models;

use think\Model;

class PayFlowModel extends Model
{
    protected $table = 'edu_pay_flow';

    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc')
    {
        $result = self::where($where)->order($order)->paginate($limit, false, ['page' => $page]);
        if (!$result->total()) {
            return ['code' => 0, 'msg' => "ok", 'count' => 0, 'data' => []];
        }
        $CourseContractModel = new CourseContractModel();
        $GradesContractModel = new GradesContractModel();
        $GoodsContractModel= new GoodsContractModel();
        foreach ($result as $key => $value) {
            $contract = ContractModel::get(['contract_number' => $value['contract_number']]);

            $arr = [];
            $res_course = $CourseContractModel->where(['contract_id' => $contract['id']])->select();
            foreach ($res_course as $item){
                $arr[] = CourseModel::get(['id' => $item['course_id']])['course_name'];
            }
            $res_grades = $GradesContractModel->where(['contract_id' => $contract['id']])->select();
            foreach ($res_grades as $item){
                $arr[] = GradesDetailsModel::get(['id' => $item['grades_id']])['grades_name'];
            }
            $res_goods = $GoodsContractModel->where(['contract_id' => $contract['id']])->select();
            foreach ($res_goods as $item){
                $arr[] = GoodsModel::get(['id' => $item['goods_id']])['goods_name'];
            }

            $result[$key]['course'] = implode('&nbsp;&nbsp;-&nbsp;&nbsp;', $arr) ? : '-';
            $result[$key]['signing_type_text'] = DictionaryModel::where('id',$contract['signing_type'])->value('name') ? : '-';
            $result[$key]['grade'] = DictionaryModel::where('id',$contract['grade'])->value('name') ? : '-';
            $result[$key]['campus'] = CampusModel::where('id',$contract['campus_id'])->value('campus_name') ? : '-';
            $result[$key]['student_name'] = $contract['student_name'] ? : '-';
            $result[$key]['phone'] = StudentModel::where('id',$contract['student_id'])->value('phone') ? : '-';
           if($value['amount'] > 0){
               $result[$key]['amount'] = '<span  style="color:#01AAED;font-weight: bold;">'. Number_format($result[$key]['amount'] / 100,2).'</span>';
           }else{
               $result[$key]['amount'] = '<span  style="color:#FF5722;font-weight: bold;">'. Number_format($result[$key]['amount'] / 100,2).'</span>';
           }


            if($value['type'] == 1){  //订单收入
                $result[$key]['contract_type_text'] = '<span  style="color:#01AAED;font-weight: bold;">订单收入</span>';
            }else if($value['type'] == 2){ //订单支出
                $result[$key]['contract_type_text'] = '<span  style="color:#FF5722;font-weight: bold;">订单退费</span>';
            }

        }
        $result = $result->toArray();
        return ['code' => 0, 'msg' => "ok", 'count' => $result['total'], 'data' => $result['data']];
    }

    /**
     * 流水类型转换
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getTypeTextAttr($value, $data)
    {
        $arr = config('payflow.type');
        return $arr[$data['type']];
    }
}